package cn.edu.suda.schoolBus.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.suda.schoolBus.bean.Driver;
import cn.edu.suda.schoolBus.bean.DriverExample;
import cn.edu.suda.schoolBus.dao.DriverMapper;
import cn.edu.suda.schoolBus.service.DriverService;
import cn.edu.suda.schoolBus.util.ResultUtil;

@Service
public class DriverServiceImpl implements DriverService {

	@Autowired
	private DriverMapper driverDao;

	private DriverExample driverExample;

	@Override
	public int addDriver(Driver driver) {
		driver.setdTime(new Date());
		return driverDao.insertSelective(driver);
	}

	@Override
	public Map<String, Object> getDriver(int page, int rows) {
		int start = (page - 1) * rows;
		driverExample = new DriverExample();
		driverExample.createCriteria().andDIdIsNotNull();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", driverDao.countByExample(driverExample));
		map.put("rows", driverDao.selectByPage(start, rows));
		return map;
	}

	@Override
	public int deleteDriver(String ids) {
		driverExample = new DriverExample();
		driverExample.createCriteria().andDIdIn(ResultUtil.getResult(ids));
		return driverDao.deleteByExample(driverExample);
	}

	@Override
	public int editDriver(Driver driver) {
		return driverDao.updateByPrimaryKeySelective(driver);
	}

	@Override
	public List<Driver> getDepartureDriver() {
		return driverDao.selectDepartureDriver();
	}

	@Override
	public int getDriver(String dNumber) {
		driverExample = new DriverExample();
		driverExample.createCriteria().andDNumberEqualTo(dNumber);
		return driverDao.selectByExample(driverExample).size();
	}

}
